# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=benchmark
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_gtest_ver=1.11.0
pkgver=1.5.6
pkgrel=2
pkgdesc="A microbenchmark support library, by Google (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/google/benchmark"
license=('APACHE')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gtest")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/google/benchmark/archive/v${pkgver}.tar.gz"
        googletest-release-${_gtest_ver}.tar.gz::https://github.com/google/googletest/archive/release-${_gtest_ver}.tar.gz
        "0001-cmake-disable-librt.patch"
        "0002-cmake-remove-werror.patch")
sha256sums=('789f85b4810d13ff803834ea75999e41b326405d83d6a538baf01499eda96102'
            'b4870bf121ff7795ba20d20bcdd8627b8e088f2d1dab299a031c1034eddc93d5'
            '1d85c2b4b1679b0ba6b9d299c89de318b6f8284d426f44ae689c76b2917fffc4'
            'd0182ff9e327106e697bcf98f52bb3ddc2d31b9ae24cc577cbe43267d392893e')

prepare() {
  cp -r googletest-release-${_gtest_ver} ${_realname}-${pkgver}/googletest

  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-cmake-disable-librt.patch"
  patch -Np1 -i "${srcdir}/0002-cmake-remove-werror.patch"
  sed -i "s|v0.0.0|v${pkgver}|g" "cmake/GetGitVersion.cmake"
}

build() {
  # Shared Build
  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DBUILD_SHARED_LIBS="ON" \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DPython_FIND_REGISTRY=NEVER \
      -DPython_ROOT_DIR=${MINGW_PREFIX} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  # Static Build
  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DBUILD_SHARED_LIBS="OFF" \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DPython_FIND_REGISTRY=NEVER \
      -DPython_ROOT_DIR=${MINGW_PREFIX} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  # Shared Check
  cd "${srcdir}/build-${MSYSTEM}-shared"
  PATH=$PWD/src:$PATH ${MINGW_PREFIX}/bin/ctest.exe ./ || true

  # Static Check
  cd "${srcdir}/build-${MSYSTEM}-static"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  # Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  # Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
